package org.hxzon.project;

import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.HashSet;

import org.springframework.beans.BeanUtils;

public class MyBeanPropertyRowMapperEx<M> extends MyBeanPropertyRowMapper<M> {
    private String _prefix;

    public MyBeanPropertyRowMapperEx(Class<M> clazz) {
        this(clazz, null);
    }

    public MyBeanPropertyRowMapperEx(Class<M> clazz, String prefix) {
        super(clazz);
        _prefix = prefix;
    }

    protected void initialize(Class<M> mappedClass) {
        if (_prefix == null) {
            super.initialize(mappedClass);
            return;
        }
        this.mappedClass = mappedClass;
        this.mappedFields = new HashMap<String, PropertyDescriptor>();
        this.mappedProperties = new HashSet<String>();
        PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
        for (PropertyDescriptor pd : pds) {
            if (pd.getWriteMethod() != null) {
                String pdName = underscoreName(pd.getName());
                this.mappedFields.put(_prefix + pdName, pd);
                this.mappedProperties.add(pd.getName());
            }
        }
    }
}
